package com.smile.server;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;

import com.google.gson.Gson;
import com.smile.constant.SendType;
import com.smile.interf.ViewInterface;
import com.smile.model.RegisterBean;
import com.smile.model.SendString;

public class ChatManager {

	private ViewInterface viewInterface;
	private Gson gson = new Gson();
	public boolean isConn = false;
	private Socket socket;
	private BufferedReader reader;
	private PrintWriter writer;

	/**
	 * 私有化构造函数，使类单例
	 */
	private ChatManager() {
	}

	private static final ChatManager CM = new ChatManager();

	public static ChatManager getCM() {
		return CM;
	}

	/**
	 * 设置视图接口，标识当前引用这个管理类的视图
	 * 
	 * @param viewInterface
	 */
	public void setViewInterface(ViewInterface viewInterface) {
		this.viewInterface = viewInterface;
	}

	/**
	 * 注册
	 * 
	 * @param username
	 * @param password
	 */
	public void register(String username, String password) {
		RegisterBean registerBean = new RegisterBean(username, password);
		String registerJson = gson.toJson(registerBean);
		SendString sendString = new SendString(SendType.REGISTER, registerJson);
		String sendJson = gson.toJson(sendString);

		writer.write(sendJson + "\n");
		writer.flush();
	}

	/**
	 * 登录
	 * 
	 * @param username
	 * @param password
	 */
	public void login(String username, String password) {
//		System.out.println(username + "--" + password);
		RegisterBean registerBean = new RegisterBean(username, password);
		String registerJson = gson.toJson(registerBean);
		SendString sendString = new SendString(SendType.LOGIN, registerJson);
		String sendJson = gson.toJson(sendString);

		writer.write(sendJson + "\n");
		writer.flush();
	}

	/**
	 * 连接服务器
	 */
	public synchronized void connect() {
		if (!isConn) {
			new Thread() {
				@Override
				public void run() {
					try {
						socket = new Socket("192.168.31.151", 8888);
						writer = new PrintWriter(new OutputStreamWriter(
								socket.getOutputStream(), "utf-8"));

						reader = new BufferedReader(new InputStreamReader(
								socket.getInputStream(), "utf-8"));
						isConn = true;
						viewInterface.setConnMessage(true, "连接成功");

						String line = "";
						while ((line = reader.readLine()) != null) {
							viewInterface.setMessage(line);
						}
						isConn = false;
						viewInterface.setConnMessage(false, "连接断开");
						writer.close();
						reader.close();
						writer = null;
						reader = null;

					} catch (Exception e) {
						isConn = false;
						viewInterface.setConnMessage(false, e.getMessage());
					}
				};
			}.start();
		}
	}

	/**
	 * 关闭socket
	 */
	public void closeSocket() {
		System.out.println("关闭");
		try {
			if (writer != null) {
				writer.close();
			}
			if (reader != null) {
				reader.close();
			}
			if (socket != null && !socket.isClosed()) {
				socket.close();
			}
		} catch (IOException e1) {
			e1.printStackTrace();
		}
	}

}
